package com.example.limit.mapper;

import com.example.limit.entity.RuleGroup;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RuleGroupMapper {
    
    @Insert("INSERT INTO rule_group(group_name, description, group_type, logic_type, condition_expression, priority, status, create_time, update_time) " +
            "VALUES(#{groupName}, #{description}, #{groupType}, #{logicType}, #{conditionExpression}, #{priority}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(RuleGroup group);
    
    @Update("UPDATE rule_group SET group_name=#{groupName}, description=#{description}, group_type=#{groupType}, " +
            "logic_type=#{logicType}, condition_expression=#{conditionExpression}, priority=#{priority}, " +
            "status=#{status}, update_time=NOW() WHERE id=#{id}")
    int update(RuleGroup group);
    
    @Delete("DELETE FROM rule_group WHERE id=#{id}")
    int delete(Long id);
    
    @Select("SELECT * FROM rule_group WHERE id=#{id}")
    RuleGroup selectById(Long id);
    
    @Select("SELECT * FROM rule_group WHERE status=1 ORDER BY priority ASC")
    List<RuleGroup> selectActiveGroups();
    
    @Select("SELECT rg.* FROM rule_group rg " +
            "JOIN purchase_rule pr ON rg.id = pr.group_id " +
            "WHERE pr.id = #{ruleId} AND rg.status = 1")
    RuleGroup selectByRuleId(Long ruleId);
}